package org.smartloli.kafka.eagle.web.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.util.IntrospectorCleanupListener;

/**
 * @author zbc
 * @date 2022/11/16
 */
@Configuration
public class RegistrationBeanConfig {

    @Bean
    public ServletListenerRegistrationBean introspectorCleanupListener() {
        ServletListenerRegistrationBean servletListenerRegistrationBean = new ServletListenerRegistrationBean(new IntrospectorCleanupListener());
        return servletListenerRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean registerEncodingFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(new CharacterEncodingFilter());
        registrationBean.setName("encodingFilter");
        registrationBean.setAsyncSupported(true);
        registrationBean.addUrlPatterns("/*");
        registrationBean.addInitParameter("encoding", "UTF-8");
        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean registerShiroFilter() {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean(new DelegatingFilterProxy());
        registrationBean.setName("shiroFilter");
        registrationBean.addUrlPatterns("/*");
        registrationBean.addInitParameter("targetBeanName", "shiroFilter");
        registrationBean.addInitParameter("targetFilterLifecycle", "true");
        return registrationBean;
    }

}